﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using XFSale.Lib;

namespace XFSale.UI
{
    using DictMetaSet = Dictionary<string, DictMeta>;

    public partial class FrmBase : UserControl
    {
        protected static Pack request = App.Request;
        protected static Pack response = App.Response;
        protected static DictMetaSet dictMeta = App.DictMeta;
        protected string param = null;

        public FrmBase()
        {
            InitializeComponent();
        }

        private string GetCtrlValue(Control ctrl)
        {
            string value = string.Empty;
            if (ctrl is TextBox)
            {
                value = (ctrl as TextBox).Text;
            }
            else if (ctrl is ComboBox)
            {
                if ((ctrl as ComboBox).SelectedItem is ValueTextPair)
                {
                    value = ((ctrl as ComboBox).SelectedItem as ValueTextPair).Value;
                }
            }
            return value;
        }

        protected bool CheckCtrlByName(Control ctrl)
        {
            string itemName = Convert.ToString(ctrl.Tag);
            if (string.IsNullOrEmpty(itemName))
            {
                throw new Error("控件没有绑定数据项");
            }

            return CheckCtrlByName(ctrl, itemName);
        }

        protected bool CheckCtrlByName(Control ctrl, string itemName)
        {
            if (string.IsNullOrEmpty(itemName))
            {
                Log.LogThrowError("数据项名称不能为空");
            }

            string key = itemName.Trim().ToUpper();
            if (!dictMeta.ContainsKey(key))
            {
                Log.LogThrowError("数据字典中没有数据项 {0}", itemName);
            }

            string value = GetCtrlValue(ctrl);
            if (string.IsNullOrEmpty(value))
            {
                value = dictMeta[key].itemDefault;
            }

            return Regex.IsMatch(value, dictMeta[key].itemVerify, RegexOptions.IgnoreCase);
        }

        protected void SetCmbVal(ComboBox cmb, string val)
        {
            foreach (object item in cmb.Items)
            {
                if (item is ValueTextPair)
                {
                    if ((item as ValueTextPair).Value == val)
                    {
                        cmb.SelectedItem = item;
                        break;
                    }
                }
            }
        }

        protected void FillCmbByName(ComboBox cmb, string itemName)
        {
            if (string.IsNullOrEmpty(itemName))
            {
                Log.LogThrowError("数据项名称不能为空");
            }

            string key = itemName.Trim().ToUpper();
            if (!dictMeta.ContainsKey(key))
            {
                Log.LogThrowError("数据字典中没有数据项 {0}", itemName);
            }

            cmb.Items.Clear();
            cmb.DisplayMember = "Text";
            cmb.ValueMember = "Value";
            foreach (DictValue item in dictMeta[key].valueItems)
            {
                ValueTextPair pair = new ValueTextPair();
                pair.Value = item.itemValue;
                pair.Text = item.itemValue + " " + item.valueDesc;
                cmb.Items.Add(pair);
                if (item.itemValue == dictMeta[key].itemDefault)
                {
                    cmb.SelectedIndex = cmb.Items.Count - 1;
                }
            }
        }

        protected void FillCmbByAuto(Control parent)
        {
            foreach (Control ctrl in parent.Controls)
            {
                if (ctrl.HasChildren)
                {
                    FillCmbByAuto(ctrl);
                    continue;
                }

                string itemName = Convert.ToString(ctrl.Tag);
                if ((ctrl is ComboBox) && !string.IsNullOrEmpty(itemName))
                {
                    FillCmbByName(ctrl as ComboBox, itemName);
                }
                else
                {
                    continue;
                }
            }
        }

        protected void FillRequestByAuto(Control parent)
        {
            foreach (Control ctrl in parent.Controls)
            {
                if (ctrl.HasChildren)
                {
                    FillRequestByAuto(ctrl);
                    continue;
                }

                string itemName = Convert.ToString(ctrl.Tag);
                if (!string.IsNullOrEmpty(itemName))
                {
                    FillRequestByName(ctrl, itemName);
                }
            }
        }

        protected void FillRequestByName(Control ctrl, string itemName)
        {
            if (string.IsNullOrEmpty(itemName))
            {
                Log.LogThrowError("数据项名称不能为空");
            }

            string key = itemName.Trim().ToUpper();
            if (!dictMeta.ContainsKey(key))
            {
                Log.LogThrowError("数据字典中没有数据项 {0}", itemName);
            }

            string value = GetCtrlValue(ctrl);
            if (string.IsNullOrEmpty(value))
            {
                value = dictMeta[key].itemDefault;
            }

            try
            {
                request.SetRecordByName(itemName, value);
            }
            catch
            {
                ctrl.Focus();
                throw new Error("输入不合法: {0}", itemName);
            }
        }

        protected void CloseFrame()
        {
            //(ParentForm.Controls["Splitter"] as DevExpress.XtraEditors.SplitContainerControl).Panel1.Enabled = true;
            //(ParentForm.Controls["NavPanel"] as DevExpress.XtraEditors.PanelControl).Enabled = true;
            this.Dispose();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
            }
        }

        protected override bool ProcessCmdKey(ref   Message msg, Keys keyData)
        {
            if (!(ActiveControl is Button) && !(ActiveControl is DevExpress.XtraEditors.BaseButton))
            {
                if (keyData == Keys.Enter || keyData == Keys.Down)
                {
                    SendKeys.Send("{TAB}");
                    return true;
                }

                if (keyData == Keys.Up)
                {
                    SendKeys.Send("+{Tab}");
                    return true;
                }
            }

            return base.ProcessCmdKey(ref   msg, keyData);
        }

    }
}
